<?php


namespace apps\marriage\app\op\controller;


use apps\marriage\core\model\MemberProfile;
use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\UserService;
use think\Request;

class MemberController extends BaseMarriageOpController
{
    /**
     * @var UserService
     */
    private $userService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    protected function init()
    {
        parent::init();
        $this->userService = service('User', SERVICE_NAMESPACE);
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $keywords = $request->param('keywords', '');
        $params = [];
        if($keywords) {
            $params['nick'] = $keywords;
        }
        $res = $this->userService->pageListByParams($params, $pageSize);
        if($res['data']) {
            foreach ($res['data'] as &$rs) {
                $rs['nick'] = strip_tags($rs['nick']);
                $rs['avatar_url'] = $this->userService->getAvatarUrl($rs['avatar'], $rs['id']);
                $rs['member'] = $this->memberProfileService->getByUserId($rs['id']);
            }
        }
        return $this->success($res);
    }

    public function lock(Request $request)
    {
        $userId = $request->param('user_id', 0, 'intval');
        $user = $this->userService->getByPk($userId);
        $this->userService->updateByPk([
            'id' => $userId,
            'is_locked' => $user['is_locked'] == 1 ? 0 : 1
        ]);
        return $this->success();
    }

    public function constants()
    {
        return $this->success([
            'genders' => MemberProfile::genders(),
            'incomes' => MemberProfile::incomes(),
            'jobs' => MemberProfile::jobs(),
            'nations' => MemberProfile::nations(),
            'blood_types' => MemberProfile::bloodTypes(),
            'kids' => MemberProfile::kids(),
            'faiths' => MemberProfile::faiths(),
            'drinks' => MemberProfile::drinks(),
            'smokes' => MemberProfile::smokes(),
            'job_types' => MemberProfile::jobTypes(),
            'education_types' => MemberProfile::educationTypes(),
            'marriage_status' => MemberProfile::marriageStatus(),
            'house_status' => MemberProfile::houseStatus(),
            'car_status' => MemberProfile::carStatus(),
            'distance_relationship_types' => MemberProfile::distanceRelationshipTypes(),
            'child_types' => MemberProfile::childTypes(),
            'live_parents_types' => MemberProfile::liveParentsTypes(),
            'zodiacs' => zodiacs(),
            'constellations' => constellations()
        ]);
    }

    public function info(Request $request)
    {
        $profileId = $request->param('profile_id', 0, 'intval');
        $user = null;
        $member = $this->memberProfileService->getByPk($profileId);
        if($member) {
            $member = $this->memberProfileService->format($member);
            $user = $this->userService->getByPk($member['user_id']);
        }
        return $this->success([
            'user' => $user,
            'member' => $member
        ]);
    }

    public function save(Request $request)
    {
        $user = $request->param('user');
        $member = $request->param('member');
        $user = json_decode($user, true);
        $member = json_decode($member, true);
        $member['expect_marriage_status'] = implode(',', $member['expect_marriage_status']);
        if($user['id'] == 0) {
            $user['password'] = rand_string(6);
            $this->memberProfileService->createByAdmin($user, $member);
        } else {
            $this->memberProfileService->updateByPk($member);
        }

        return $this->success();
    }

    public function recommend(Request $request)
    {
        $memberId = $request->param('member_id', 0, 'intval');
        $recommendType = $request->param('recommend_type', 0, 'intval');
        $this->memberProfileService->updateByPk([
            'id' => $memberId,
            'recommend_type' => $recommendType
        ]);
        return $this->success();
    }

    public function live_room_bg(Request $request)
    {
        $memberId = $request->param('member_id', 0, 'intval');
        $url = $this->memberProfileService->getLiveRoomBgImage($memberId);
        return $this->success([
            'url' => $url
        ]);
    }
}